/*
 * This file is part of Shopping Lists Organizer software
 *
 * Copyright (C) 2011 Denis Kvita <dkvita@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "chartview.h"

#include <QtGui>

#include "nightcharts.h"
#include "expensesmodel.h"

chartView::chartView(QWidget *parent) :
	QAbstractScrollArea(parent)
{
	d= 0;
	p= 0;
	chart= new Nightcharts;
	typeCB= new QComboBox(this);
	typeCB->move(3,3);
	typeCB->addItem(tr("Pie"));
	typeCB->addItem(tr("3D Pie"));
	typeCB->addItem(tr("Histogram"));
	typeCB->show();
	connect(typeCB, SIGNAL(activated(int)), this, SLOT(onChartTypeChanged(int)));
}

chartView::~chartView()
{
	delete chart;
}

void chartView::init(expensesModel *dataModel, QSortFilterProxyModel *proxyModel, double total)
{

	d= dataModel;
	if(p==0){
		p= new QSortFilterProxyModel;
		p->setSourceModel(d);
	}
	p->invalidate();
	p->setSortRole(Qt::EditRole);
	p->setFilterRegExp(proxyModel->filterRegExp());
	p->sort(3, Qt::DescendingOrder);

	chart->clear();
	if(typeCB->currentIndex() ==0 ) chart->setType(Nightcharts::Pie);
	else if(typeCB->currentIndex() == 1) chart->setType(Nightcharts::Dpie);
	else if(typeCB->currentIndex() == 2) chart->setType(Nightcharts::Histogramm);
	chart->setLegendType(Nightcharts::Vertical);//{Round,Vertical}
	chart->setCords(50,50,this->width()/1.5,this->height()/1.5);

	float totalPerc= 0;
	float other= 0.0;
	int otherCnt= 0;
	float v;
	QColor lc= Qt::green;
	for(int r=0; r<p->rowCount(); r++){
		v= p->mapFromSource(d->index(p->mapToSource(p->index(r,0)).row(),3)).data().toDouble();
		if(v <= 0) continue;
		float perc= (int)(v/total*1000);
		perc/= 10;
		if(perc<=1){
			otherCnt++;
			other+= v;
			continue;
		}
		totalPerc+= perc;
		chart->addPiece(p->mapFromSource(d->index(p->mapToSource(p->index(r,0)).row(),0)).data().toString()
						+" ("+QString::number(v,'f',2)+")"
						,lc
						,perc);
	}
	if(other>0){
		v= (int)(other/total*1000);
		v/= 10;
		totalPerc+= v;
		chart->addPiece(tr("Other %1 items (").arg(otherCnt)+QString::number(other,'f',2)+")"
						,lc
						,v);
	}

	if(totalPerc < 100) chart->setPiecePercentage(0,chart->piecePercentage(0)+100-totalPerc);

	for(int i=0; i<chart->pieceCount(); i++) chart->setPieceColor(i,QColor::fromHsv((lc.toHsv().hue()+360/chart->pieceCount()*i)%360,lc.toHsv().saturation(),lc.toHsv().value()));
	viewport()->update();
}

void chartView::onChartTypeChanged(int t)
{
	if(t ==0 ) chart->setType(Nightcharts::Pie);
	else if(t == 1) chart->setType(Nightcharts::Dpie);
	else if(t == 2) chart->setType(Nightcharts::Histogramm);
	viewport()->update();
}

void chartView::paintEvent(QPaintEvent * e)
{
	QWidget::paintEvent(e);
	QPainter painter;
	QFont font;
	painter.begin(viewport());
	chart->draw(&painter);
	chart->drawLegend(&painter);
}

